package org.project.hint_game;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/** Room Numbers
0: Kitchen
1: Dining Hall
2: Library
3: Ballroom
4: Bedroom
5: Courtyard
6: Dungeon
7: Armory
**/

public class MainRoomActivity extends Activity{
	// UI Elements
	private ImageButton personSelector_;
	private ImageButton weaponSelector_;
	private ImageButton mapImgButton_;
	private ImageButton actionImgButton_;
	private ImageButton hintImgButton_;
	private Button refreshButton_;
	private ImageView yourCharImg_;
	
	// Other Variables
	private ArrayList<Integer> charImgList_;
	private ArrayList<Integer> wpnImgList_;
	private ArrayAdapter<String> usersList_;
	private int curCharImg_;
	private int curWpnImg_;
	private int curRoom_;
	private int[] qReady_;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_room);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        
        // Set up background based on room select activity
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.mainRoomLayout);
        curRoom_ = this.getIntent().getIntExtra("roomChoice", 0);
        if (curRoom_ == 0)
        	relativeLayout.setBackgroundResource(R.drawable.room_kitchen);
        else if (curRoom_ == 1)
        	relativeLayout.setBackgroundResource(R.drawable.room_dininghall);
        else if (curRoom_ == 2)
        	relativeLayout.setBackgroundResource(R.drawable.room_library);
        else if (curRoom_ == 3)
        	relativeLayout.setBackgroundResource(R.drawable.room_ballroom);
        else if (curRoom_ == 4)
        	relativeLayout.setBackgroundResource(R.drawable.room_bedroom);
        else if (curRoom_ == 5)
        	relativeLayout.setBackgroundResource(R.drawable.room_courtyard);
        else if (curRoom_ == 6)
        	relativeLayout.setBackgroundResource(R.drawable.room_dungeon);
        else if (curRoom_ == 7)
        	relativeLayout.setBackgroundResource(R.drawable.room_armory);
        
        // Initialize Variables
        personSelector_ = (ImageButton) findViewById(R.id.pImageButton);
        weaponSelector_ = (ImageButton) findViewById(R.id.wImageButton);
        mapImgButton_ = (ImageButton) findViewById(R.id.mapImgButton);
        actionImgButton_ = (ImageButton) findViewById(R.id.actionImgButton);
        hintImgButton_ = (ImageButton) findViewById(R.id.hintImgButton);
        yourCharImg_ = (ImageView) findViewById(R.id.yourCharImgView);
        refreshButton_ = (Button) findViewById(R.id.refreshButton);
        usersList_ = new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice);
        charImgList_ = new ArrayList<Integer>();
        wpnImgList_ = new ArrayList<Integer>();
        curCharImg_ = 0;
        curWpnImg_ = 0;
        qReady_ = new int[2];
        // Populate Lists
        charImgList_.add(R.drawable.char_fairy);
        charImgList_.add(R.drawable.char_sailor);
        charImgList_.add(R.drawable.char_ghost);
        charImgList_.add(R.drawable.char_gnome);
        charImgList_.add(R.drawable.char_magician);
        charImgList_.add(R.drawable.char_witch);
        wpnImgList_.add(R.drawable.weapon_axe);
        wpnImgList_.add(R.drawable.weapon_sword);
        wpnImgList_.add(R.drawable.weapon_spear);
        wpnImgList_.add(R.drawable.weapon_bowarrow);
        wpnImgList_.add(R.drawable.weapon_poison);
        wpnImgList_.add(R.drawable.weapon_mace);
        usersList_.add("RobinHood");
        usersList_.add("LittleJohn");
        usersList_.add("MaidMarian");
        
        // OnClick Handlers for buttons
        personSelector_.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	// Notify action button
            	qReady_[0] = 1;
            	if (qReady_[0] == 1 && qReady_[1] == 1){
            		actionImgButton_.setImageResource(R.drawable.button_question);
            	}
                // Change Character Image
            	personSelector_.setImageResource(charImgList_.get(curCharImg_));
            	curCharImg_++;
            	if (curCharImg_ >= charImgList_.size())
            		curCharImg_ = 0;
            }
        });
        weaponSelector_.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Notify action button
				qReady_[1] = 1;
				if (qReady_[0] == 1 && qReady_[1] == 1){
					actionImgButton_.setImageResource(R.drawable.button_question);
            	}
				// Change Weapon Image
				weaponSelector_.setImageResource(wpnImgList_.get(curWpnImg_));
				curWpnImg_++;
            	if (curWpnImg_ >= wpnImgList_.size())
            		curWpnImg_ = 0;
			}
		});
        mapImgButton_.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v){
        		// Go to RoomSelectActivity
        		Intent intent = new Intent(MainRoomActivity.this, RoomSelectActivity.class);
        		intent.putExtra("currentRoom", curRoom_);
            	startActivity(intent);
        	}
        });
        actionImgButton_.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(qReady_[0] == 1 && qReady_[1] == 1){
					// Do stuff
					pickUser();
				}
			}
		});
        hintImgButton_.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v){
        		Intent i = new Intent(MainRoomActivity.this, CheckSheetActivity.class);
        		i.putExtra("currentRoom", curRoom_);
        		startActivity(i);
        	}
        });
        refreshButton_.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				refreshPressed();
			}
		});
	}
	
	public void refreshPressed(){
		
	}
	
	protected void onResume(){
		super.onResume();
		qReady_[0] = -1;
		qReady_[1] = -1;
	}
	
	protected void onStop() {
	    super.onStop();
	}

	protected void onDestroy() {
	    super.onDestroy();
	}
	
	private void pickUser(){
	    AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    builder.setTitle("Question who?");
	    builder.setAdapter(usersList_, new DialogInterface.OnClickListener() {
	    	public void onClick(DialogInterface dialog, int which) {
	    		// The 'which' argument contains the index position
	            // of the selected item
	    		Log.d("Selection", Integer.toString(which));
	        }
	    });
	    builder.create();
	    builder.show();
	}
	@Override
	public void onBackPressed() {
	}
}